<?php
namespace app\admin\model;
use app\common\model\CommonModel;
use app\common\model\Rule;
use app\common\model\Group;
use think\Session;

class RoleModel extends CommonModel
{
	/**
	* 添加角色信息
	*/
	public static function setRole($info)
	{
		if(!array_key_exists('id',$info))
		{
			return back(config('state.STATUS_CODE_THREE'),config('state.STATUS_MSG_THREE'));
		}
		$data['shop_id'] = Session::get('admin.shop_id');
		$data['title'] = $info['title'];
		$data['rules'] = implode(',',$info['id']);
		$data['admin_type'] = Session::get('admin.admin_type');
		$data['remarks'] = $info['remarks'];
		if(!(new Group())->serGroup($data))
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
	/**
	* 修改角色信息
	*/
	public static function updateRole($info)
	{
		if(!array_key_exists('id',$info))
		{
			return back(config('state.STATUS_CODE_THREE'),config('state.STATUS_MSG_THREE'));
		}
		$data['title'] = $info['title'];
		$data['rules'] = implode(',',$info['id']);
		$data['admin_type'] = Session::get('admin.admin_type');
		$data['remarks'] = $info['remarks'];
		if(!(new Group())->updateRole(['agid'=>$info['aid']],$data))
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
	/**
	* 删除
	*/
	public static function delRole($id)
	{
		if((new Group())->delGroup(['agid'=>$id]) === false)
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
	/**
	* 获取规则信息
	*/
	public static function ruledata($rules = null)
	{
		$rulearr = [];
		if($rules)
		{
			$rulearr = explode(',',$rules);
		}
		
		$Rule = new Rule();
		$where['pid'] = CommonModel::NUMBER_ZERO;
		$where['ascription'] = Session::get('admin.admin_type')==CommonModel::NUMBER_ZERO?CommonModel::NUMBER_ZERO:CommonModel::NUMBER_ONE;
		$data = $Rule->ruleList($where,'arid,title');
		foreach($data as $k => $v)
		{
			if($rulearr)
			{	if(in_array($v['arid'],$rulearr))
				{
					$data[$k]['checked'] = 'true';
				}else{
					$data[$k]['checked'] = 'false';
				}
				$data[$k]['esrule'] = self::esrule($Rule,$v['arid'],$rulearr);
			}else{
				$data[$k]['esrule'] = self::esrule($Rule,$v['arid']);
			}
		}
		return $data;
	}
	/**
	* 获取二三级规则信息
	*/
	private static function esrule($obj,$arid,$rulearr = [])
	{
		$data = $obj->ruleList(['pid'=>$arid],'arid,title');
		foreach($data as $k=>$vo)
		{
			$data[$k]['xiaji'] = $obj->ruleList(['pid'=>$vo['arid']],'arid,title');
		}
		if($rulearr)
		{
			foreach($data as $k=>$vo)
			{
				if(in_array($vo['arid'],$rulearr))
				{
					$data[$k]['checked'] = 'true';
				}else{
					$data[$k]['checked'] = 'false';
				}
				/*下级*/
				foreach($vo['xiaji'] as $i=>$v)
				{
					if(in_array($v['arid'],$rulearr))
					{
						$data[$k]['xiaji'][$i]['checked'] = 'true';
					}else{
						$data[$k]['xiaji'][$i]['checked'] = 'false';
					}
				}
			}
		}
		return $data;
	}
}
